import { TokenManager as Token } from "maishu-node-auth/out/token";
import { constants } from "maishu-node-auth";
// let constants = {
//     anonymousRoleId: "738FB92C-60CF-4280-B5AE-61C376D0AADF",

// }
//===========================================
// 目标主机，服务所在的主机
const target_host = '127.0.0.1';
//===========================================
let localhost = "127.0.0.1";
let imageServiceProt = 4010;
let adminProt = 4512;
let shopPort = 4513;
let designerPort = 4421;

export let config = {
    designer: {
        port: designerPort
    },
    shop: {
        port: shopPort
    },
    admin: {
        port: adminProt,
    },
    nodeAuth: {
        // bindIP: "127.0.0.1",
        port: 2856,
        db: {
            user: "root",
            password: "81263",
            database: "shop_auth",
            host: "localhost",
            port: 3306
        },
        adminMobile: "18502146746",
        adminPassword: "b6d767d2f8ed5d21a44b0e5886680cb9", // md5 密码，明文为 22
        proxy: {
            '^/AdminSite/(\\S+)': { targetUrl: `http://${target_host}:9000/Admin/$1`, headers: proxyHeader },
            '^/AdminStock/(\\S+)': { targetUrl: `http://${target_host}:9005/Admin/$1`, headers: proxyHeader },
            '^/AdminShop/(\\S+)': { targetUrl: `http://${target_host}:9010/Admin/$1`, headers: proxyHeader },
            '^/AdminMember/(\\S+)': { targetUrl: `http://${target_host}:9020/Admin/$1`, headers: proxyHeader },
            '^/AdminWeiXin/(\\S+)': { targetUrl: `http://${target_host}:9030/Admin/$1`, headers: proxyHeader },
            '^/AdminAccount/(\\S+)': { targetUrl: `http://${target_host}:9035/Admin/$1`, headers: proxyHeader },
            '^/UserSite/(\\S+)': { targetUrl: `http://${target_host}:9000/User/$1`, headers: proxyHeader },
            '^/UserStock/(\\S+)': { targetUrl: `http://${target_host}:9005/User/$1`, headers: proxyHeader },
            '^/UserShop/(\\S+)': { targetUrl: `http://${target_host}:9010/User/$1`, headers: proxyHeader },
            '^/UserMember/(\\S+)': { targetUrl: `http://${target_host}:9020/User/$1`, headers: proxyHeader },
            '^/UserWeiXin/(\\S+)': { targetUrl: `http://${target_host}:9030/User/$1`, headers: proxyHeader },
            '^/UserAccount/(\\S+)': { targetUrl: `http://${target_host}:9035/User/$1`, headers: proxyHeader },
            "^/image(\\S+)": `http://127.0.0.1:${imageServiceProt}$1`,
            "^/designer(\\S+)": `http://127.0.0.1:${designerPort}$1`,
            "^/admin(\\S*)": `http://127.0.0.1:${adminProt}$1`,
            "^/shop(\\S*)": `http://127.0.0.1:${shopPort}$1`,
            "^/chitu-admin(\\S*)": `http://127.0.0.1:4612$1`
        },
        permissions: {
            "*.js": [constants.anonymousRoleId],
            "*.html": [constants.anonymousRoleId],
            "*.css": [constants.anonymousRoleId],
            "*.less": [constants.anonymousRoleId],
            "*.jpg": [constants.anonymousRoleId],
            "*.png": [constants.anonymousRoleId],

            "/admin(/*)": [constants.anonymousRoleId],
            "/user/*": [constants.anonymousRoleId],
            "/designer/*": [constants.anonymousRoleId],
            "/shop(/*)": [constants.anonymousRoleId],
            "/chitu-admin(/*)": [constants.anonymousRoleId],
            "/auth(/*)": [constants.anonymousRoleId],

        }
    },
    imageService: {
        port: imageServiceProt,
        db: {
            host: "127.0.0.1",
            database: "shop_image",
            port: 3306,
            user: "root",
            password: "81263"
        }

    }
}

/**
 * 
 * @param {http.IncomingMessage} req 
 */
async function proxyHeader(req) {
    let header = {}
    let tokenText = req.headers['token']
    if (tokenText) {
        try {
            let token = await Token.parse(tokenText);
            var obj = JSON.parse(token.content);
            header = obj
        } catch (err) {
            console.error(err)
        }
    }


    if (header["user_id"]) {
        header['SellerId'] = header["user_id"]
        header['UserId'] = header["user_id"]
    }

    return header
}
